﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SuchSofts.OpenImage.Lib.Image;

namespace SuchSofts.OpenImage.Dialogs
{
    internal partial class FormNew : Form
    {
        private readonly string typesValidFailedMessage;

        public bool IsFinished { get; private set; }
        public ImageFilter Filter { get; private set; }
        public string Page { get { return textBoxPage.Text; } }
        public string Folder { get { return textBoxFolder.Text; } }
        public bool IsDownloadNow { get { return checkBoxIsDownloadNow.Checked; } }

        public FormNew(string page,string folder, ImageFilter filter)
        {
            InitializeComponent();
            this.IsFinished = false;
            this.Filter = filter;
            textBoxPage.Text = page;
            textBoxFolder.Text = folder;
            propertyGridFilter.SelectedObject = this.Filter;

            //Generate TYPES_VALID_FAILED_MESSAGE
            string allTypesString = ImageFilter.GenerateTypesString();
            StringBuilder messageBuilder = new StringBuilder("Types string you provided is not valid.\nThe types string must contain at least one type listed below:\n");
            messageBuilder.Append(allTypesString.Replace("|", " "));
            messageBuilder.Append("\nThe strings for different types must be separated by the vertical bar (|).\nExample: ");
            messageBuilder.Append(allTypesString);
            typesValidFailedMessage = messageBuilder.ToString();
        }

        private void buttonDownload_Click(object sender, EventArgs e)
        {
            if (textBoxPage.Text.Trim() == "")
            {
                MessageBox.Show("Please enter a page address.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
                textBoxPage.SelectAll();
                textBoxPage.Focus();
                return;
            }
            if (textBoxFolder.Text.Trim() == "")
            {
                MessageBox.Show("Please enter a folder path.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
                textBoxFolder.SelectAll();
                textBoxFolder.Focus();
                return;
            }
            if (!textBoxFolder.Text.EndsWith("\\"))
            {
                textBoxFolder.Text += "\\";
            }
            this.IsFinished = true;
            this.Close();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.IsFinished = false;
            this.Close();
        }

        private void propertyGridFilter_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
        {
            if (e.ChangedItem.Label == "Types")
            {
                string types = e.ChangedItem.Value.ToString();
                if (!ImageFilter.IsLeagalTypesString(ref types))
                {
                    MessageBox.Show(typesValidFailedMessage, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    Filter.Types = ImageFilter.GenerateTypesString();
                }
            }
            if (e.ChangedItem.Label == "MinimumSize" || e.ChangedItem.Label == "MaximumSize" || e.ChangedItem.Label == "Width" || e.ChangedItem.Label=="Height")
            {
                if (Filter.MinimumSize.Width < 0)
                {
                    Filter.MinimumSize = new Size(0, Filter.MinimumSize.Height);
                }
                if (Filter.MinimumSize.Height < 0)
                {
                    Filter.MinimumSize = new Size(Filter.MinimumSize.Width, 0);
                }
                if (Filter.MaximumSize.Width < Filter.MinimumSize.Width)
                {
                    Filter.MaximumSize = new Size(Filter.MinimumSize.Width, Filter.MaximumSize.Height);
                }
                if (Filter.MaximumSize.Height < Filter.MinimumSize.Height)
                {
                    Filter.MaximumSize = new Size(Filter.MaximumSize.Width, Filter.MinimumSize.Height);
                }
            }
            propertyGridFilter.Refresh();
        }

        private void buttonBrowse_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialogSavedFolder.ShowDialog() == DialogResult.OK)
            {
                textBoxFolder.Text = folderBrowserDialogSavedFolder.SelectedPath;
            }
        }

        private void textBoxFolder_TextChanged(object sender, EventArgs e)
        {
            Properties.Settings.Default.DefaultFolder = textBoxFolder.Text;
        }

        private void linkLabelAllowedTypes_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            MessageBox.Show("Allowed types:\n" + ImageFilter.GenerateTypesString().Replace("|", " "), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}
